package com.smartthinking.cypqro;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class Play extends Service implements MediaPlayer.OnPreparedListener{

	MediaPlayer mp;
	AudioManager audio;
	Context context;
	String ban_destino_url,ban_url;
	ImageView banner;
	private static final String TAG = "AudioPlayer";
	private Handler handler = new Handler();

	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	public void onCreate()
	{
//			if(mp.isPlaying()){
//			}else{
//				mp.start();
//			}

				mp = new MediaPlayer();
				mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//				audio=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//				int max=audio.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
//				mp.setVolume(max, max);
				AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
				int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
				mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
				mp.setOnPreparedListener(this);
				mp.setOnPreparedListener(new OnPreparedListener() {
			        public void onPrepared(MediaPlayer mp) {
			            mp.start();
			        }
			    });
				
			Log.i("ENTR A MEDIAPLAYER", "ENTRO");

			try {
//						mp.setDataSource("http://184.154.74.51:8096/");
				mp.setDataSource("http://173.243.114.220:9864/");
//				mp.setDataSource("http://knight.wavestreamer.com:7539");
				
						mp.prepare();
				        mp.start();
					} catch (IOException e) {
						e.printStackTrace();
					}
		
		super.onCreate();
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}
	public void start() {
		mp.start();
	}
	public void pause() {
		mp.pause();
	}
	public int getDuration() {
		return mp.getDuration();
	}
	public int getCurrentPosition() {
		return mp.getCurrentPosition();
	}
	public void seekTo(int i) {
	}
	public boolean isPlaying() {
		return mp.isPlaying();
	}
	public void stop(){
		mp.stop(); 
	}
	public boolean canStop(){
		return true;
	}
	public boolean canPause() {
		return true;
	}
	public boolean canSeekBackward() {
		return false;
	}
	public boolean canSeekForward() {
		return false;
	}
	public void setVolume(float volumeLeft, float volumeRight) {
		mp.setVolume(volumeLeft, volumeRight);
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		//		mp.stop();
//		mp.pause();
		if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
            }
            mp.release();
        }
	}
	
		public void onPrepared(MediaPlayer player) {
			Log.d(TAG, "onPrepared");
//			mp.start();
			mp.setOnPreparedListener(Play.this);
			handler.post(new Runnable() {
				public void run() {
				}
			});
			
		}
	
//	public void setVolume(float volumeLeft, float volumeRight) {
//	}


}